﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;

namespace iPhoneAppGenre.models
{
    class ipaFile
    {
        private int _dbid;
        public int dbid
        {
            get { return _dbid; }
            set { _dbid = value; }
        }
        #region GETTERS & SETTERS
        private string _ipaName;
        public string ipaName
        {
            get { return _ipaName; }
            set { _ipaName = value; }
        }
        private string _ipaExt;
        public string ipaExt
        {
            get { return _ipaExt; }
            set { _ipaExt = value; }
        }
        private string _ipaNameShort;
        public string ipaNameShort
        {
            get { return _ipaNameShort; }
            set { _ipaNameShort = value; }
        }
        private string _ipaLocation;
        public string ipaLocation
        {
            get { return _ipaLocation; }
            set { _ipaLocation = value; }
        }
        public string ipaLocationName
        {
            get { return _ipaLocation + "\\" + _ipaName + ".ipa"; }
        }
        private string _ipaGenreName;
        public string ipaGenreName
        {
            get { return _ipaGenreName; }
            set { _ipaGenreName = value; }
        }
        private string _ipaGenreCode;
        public string ipaGenreCode
        {
            get { return _ipaGenreCode; }
            set { _ipaGenreCode = value; }
        }
        private string _ipaSeller;
        public string ipaSeller
        {
            get { return _ipaSeller; }
            set { _ipaSeller = value; }
        }
        private bool _ipaHasMetadata;
        public bool ipaHasMetadata
        {
            get { return _ipaHasMetadata; }
            set { _ipaHasMetadata = value; }
        }
        #endregion

        private ZipFile ipaZip;

        public ipaFile(string ipsPathFileName)
        {
            FileInfo ipaFile = new FileInfo(ipsPathFileName);
            this._ipaName = ipaFile.Name.Replace(".ipa", "");
            this._ipaNameShort = this._ipaName.Replace(" ", "");
            this._ipaExt = ".ipa";
            this._ipaLocation = ipaFile.DirectoryName;

            ipaZip = new ZipFile(ipaLocationName);
            if (ipaZip.FindEntry("iTunesMetadata.plist", true) == -1)
            {
                _ipaHasMetadata = false;
            }
            else
            {
                _ipaHasMetadata = true;
            }
            _ipaGenreName = "";
            _ipaGenreCode = "";
            _ipaSeller = Properties.Settings.Default.ipaDefaultSeller;


            
        }

        public void updateIPA()
        {
            String ipaMetaData = Properties.Resources.iTunesMetadata;
            ipaMetaData = ipaMetaData.Replace("my app", _ipaName);
            ipaMetaData = ipaMetaData.Replace("myapp", _ipaNameShort);

            ipaMetaData = ipaMetaData.Replace("mygenre", _ipaGenreName);
            ipaMetaData = ipaMetaData.Replace("0000", _ipaGenreCode);

            using (StreamWriter sw = new StreamWriter("c:\\iTunesMetadata.plist"))
            {
                // Add some text to the file.
                sw.Write(ipaMetaData);
            }

            ipaZip = new ZipFile(ipaLocationName);
            ipaZip.BeginUpdate();
            ipaZip.Add("c:\\iTunesMetadata.plist");
            try
            {
                ipaZip.CommitUpdate();
            }
            catch
            {
                throw new ExceptionSaveMetadata(ipaLocationName);
            } finally {
                File.Delete("c:\\iTunesMetadata.plist");
                ipaZip.Close();
            }
        }
    }
}